package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.CureMedicineDAO;
import cn.edu.scau.cmi.hombio.dao.CureMethodDAO;
import cn.edu.scau.cmi.hombio.dao.CureTaskCourseDAO;
import cn.edu.scau.cmi.hombio.dao.CureTypeDAO;

import cn.edu.scau.cmi.hombio.domain.CureMedicine;
import cn.edu.scau.cmi.hombio.domain.CureMethod;
import cn.edu.scau.cmi.hombio.domain.CureTaskCourse;
import cn.edu.scau.cmi.hombio.domain.CureType;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for CureMethod entities
 * 
 */

@Service("CureMethodService")

@Transactional
public class CureMethodServiceImpl implements CureMethodService {

	/**
	 * DAO injected by Spring that manages CureMedicine entities
	 * 
	 */
	@Autowired
	private CureMedicineDAO cureMedicineDAO;

	/**
	 * DAO injected by Spring that manages CureMethod entities
	 * 
	 */
	@Autowired
	private CureMethodDAO cureMethodDAO;

	/**
	 * DAO injected by Spring that manages CureTaskCourse entities
	 * 
	 */
	@Autowired
	private CureTaskCourseDAO cureTaskCourseDAO;

	/**
	 * DAO injected by Spring that manages CureType entities
	 * 
	 */
	@Autowired
	private CureTypeDAO cureTypeDAO;

	/**
	 * Instantiates a new CureMethodServiceImpl.
	 *
	 */
	public CureMethodServiceImpl() {
	}

	/**
	 * Save an existing CureMedicine entity
	 * 
	 */
	@Transactional
	public CureMethod saveCureMethodCureMedicines(Integer id, CureMedicine related_curemedicines) {
		CureMethod curemethod = cureMethodDAO.findCureMethodByPrimaryKey(id, -1, -1);
		CureMedicine existingcureMedicines = cureMedicineDAO.findCureMedicineByPrimaryKey(related_curemedicines.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcureMedicines != null) {
			existingcureMedicines.setId(related_curemedicines.getId());
			existingcureMedicines.setQuantity(related_curemedicines.getQuantity());
			existingcureMedicines.setMemo(related_curemedicines.getMemo());
			related_curemedicines = existingcureMedicines;
		}

		related_curemedicines.setCureMethod(curemethod);
		curemethod.getCureMedicines().add(related_curemedicines);
		related_curemedicines = cureMedicineDAO.store(related_curemedicines);
		cureMedicineDAO.flush();

		curemethod = cureMethodDAO.store(curemethod);
		cureMethodDAO.flush();

		return curemethod;
	}

	/**
	 * Delete an existing CureMethod entity
	 * 
	 */
	@Transactional
	public void deleteCureMethod(CureMethod curemethod) {
		cureMethodDAO.remove(curemethod);
		cureMethodDAO.flush();
	}

	/**
	 * Delete an existing CureType entity
	 * 
	 */
	@Transactional
	public CureMethod deleteCureMethodCureType(Integer curemethod_id, Integer related_curetype_id) {
		CureMethod curemethod = cureMethodDAO.findCureMethodByPrimaryKey(curemethod_id, -1, -1);
		CureType related_curetype = cureTypeDAO.findCureTypeByPrimaryKey(related_curetype_id, -1, -1);

		curemethod.setCureType(null);
		related_curetype.getCureMethods().remove(curemethod);
		curemethod = cureMethodDAO.store(curemethod);
		cureMethodDAO.flush();

		related_curetype = cureTypeDAO.store(related_curetype);
		cureTypeDAO.flush();

		cureTypeDAO.remove(related_curetype);
		cureTypeDAO.flush();

		return curemethod;
	}

	/**
	 * Load an existing CureMethod entity
	 * 
	 */
	@Transactional
	public Set<CureMethod> loadCureMethods() {
		return cureMethodDAO.findAllCureMethods();
	}

	/**
	 */
	@Transactional
	public CureMethod findCureMethodByPrimaryKey(Integer id) {
		return cureMethodDAO.findCureMethodByPrimaryKey(id);
	}

	/**
	 * Save an existing CureTaskCourse entity
	 * 
	 */
	@Transactional
	public CureMethod saveCureMethodCureTaskCourse(Integer id, CureTaskCourse related_curetaskcourse) {
		CureMethod curemethod = cureMethodDAO.findCureMethodByPrimaryKey(id, -1, -1);
		CureTaskCourse existingcureTaskCourse = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(related_curetaskcourse.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcureTaskCourse != null) {
			existingcureTaskCourse.setId(related_curetaskcourse.getId());
			existingcureTaskCourse.setStep(related_curetaskcourse.getStep());
			existingcureTaskCourse.setTime(related_curetaskcourse.getTime());
			existingcureTaskCourse.setMemo(related_curetaskcourse.getMemo());
			related_curetaskcourse = existingcureTaskCourse;
		}

		curemethod.setCureTaskCourse(related_curetaskcourse);
		related_curetaskcourse.getCureMethods().add(curemethod);
		curemethod = cureMethodDAO.store(curemethod);
		cureMethodDAO.flush();

		related_curetaskcourse = cureTaskCourseDAO.store(related_curetaskcourse);
		cureTaskCourseDAO.flush();

		return curemethod;
	}

	/**
	 * Delete an existing CureTaskCourse entity
	 * 
	 */
	@Transactional
	public CureMethod deleteCureMethodCureTaskCourse(Integer curemethod_id, Integer related_curetaskcourse_id) {
		CureMethod curemethod = cureMethodDAO.findCureMethodByPrimaryKey(curemethod_id, -1, -1);
		CureTaskCourse related_curetaskcourse = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(related_curetaskcourse_id, -1, -1);

		curemethod.setCureTaskCourse(null);
		related_curetaskcourse.getCureMethods().remove(curemethod);
		curemethod = cureMethodDAO.store(curemethod);
		cureMethodDAO.flush();

		related_curetaskcourse = cureTaskCourseDAO.store(related_curetaskcourse);
		cureTaskCourseDAO.flush();

		cureTaskCourseDAO.remove(related_curetaskcourse);
		cureTaskCourseDAO.flush();

		return curemethod;
	}

	/**
	 * Return all CureMethod entity
	 * 
	 */
	@Transactional
	public List<CureMethod> findAllCureMethods(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<CureMethod>(cureMethodDAO.findAllCureMethods(startResult, maxRows));
	}

	/**
	 * Save an existing CureMethod entity
	 * 
	 */
	@Transactional
	public void saveCureMethod(CureMethod curemethod) {
		CureMethod existingCureMethod = cureMethodDAO.findCureMethodByPrimaryKey(curemethod.getId());

		if (existingCureMethod != null) {
			if (existingCureMethod != curemethod) {
				existingCureMethod.setId(curemethod.getId());
				existingCureMethod.setMemo(curemethod.getMemo());
			}
			curemethod = cureMethodDAO.store(existingCureMethod);
		} else {
			curemethod = cureMethodDAO.store(curemethod);
		}
		cureMethodDAO.flush();
	}

	/**
	 * Return a count of all CureMethod entity
	 * 
	 */
	@Transactional
	public Integer countCureMethods() {
		return ((Long) cureMethodDAO.createQuerySingleResult("select count(o) from CureMethod o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing CureType entity
	 * 
	 */
	@Transactional
	public CureMethod saveCureMethodCureType(Integer id, CureType related_curetype) {
		CureMethod curemethod = cureMethodDAO.findCureMethodByPrimaryKey(id, -1, -1);
		CureType existingcureType = cureTypeDAO.findCureTypeByPrimaryKey(related_curetype.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcureType != null) {
			existingcureType.setId(related_curetype.getId());
			existingcureType.setName(related_curetype.getName());
			existingcureType.setMemo(related_curetype.getMemo());
			related_curetype = existingcureType;
		}

		curemethod.setCureType(related_curetype);
		related_curetype.getCureMethods().add(curemethod);
		curemethod = cureMethodDAO.store(curemethod);
		cureMethodDAO.flush();

		related_curetype = cureTypeDAO.store(related_curetype);
		cureTypeDAO.flush();

		return curemethod;
	}

	/**
	 * Delete an existing CureMedicine entity
	 * 
	 */
	@Transactional
	public CureMethod deleteCureMethodCureMedicines(Integer curemethod_id, Integer related_curemedicines_id) {
		CureMedicine related_curemedicines = cureMedicineDAO.findCureMedicineByPrimaryKey(related_curemedicines_id, -1, -1);

		CureMethod curemethod = cureMethodDAO.findCureMethodByPrimaryKey(curemethod_id, -1, -1);

		related_curemedicines.setCureMethod(null);
		curemethod.getCureMedicines().remove(related_curemedicines);

		cureMedicineDAO.remove(related_curemedicines);
		cureMedicineDAO.flush();

		return curemethod;
	}
}
